import Immutable from 'immutable';
import { routerRedux } from 'dva/router';

// import toast from '../utils/toast';
// import { delay } from '../utils/func';

import { exchangeCard } from '../services/card';

export default {
  namespace: 'card',
  state: Immutable.fromJS({
    error: null,
  }),

  effects: {
    *exchangeCard({ payload }, { call, put }) {
      const res = yield call(exchangeCard, payload);
      if (res.success) {
        yield put(routerRedux.push('result?type=vip'));
      } else {
        yield put({
          type: 'updateErr',
          payload: res.msg,
        });
      }
    },
  },

  reducers: {
    clearError(state) {
      return state.set('error', null);
    },
    updateErr(state, { payload }) {
      return state.set('error', payload);
    },
  },
};
